/**
 * 
 */
package info.niwota.home;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.Window;
import android.webkit.CookieSyncManager;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

/**
 * @author qiangli
 * 
 */
public class WebBrowser extends Activity {
	public static final int MENU_HOME =  R.id.browser_home_opt_menu_id;
	public static final int MENU_REFRESH = R.id.browser_refresh_opt_menu_id;
	public static final int MENU_STOP = R.id.browser_stop_opt_menu_id;
	public static final int MENU_BACK = R.id.browser_back_opt_menu_id;
	public static final int MENU_FORWARD = R.id.browser_forward_opt_menu_id;

	final private class MyWebChromeClient extends WebChromeClient {

		@Override
		public void onProgressChanged(WebView view, int newProgress) {
			getWindow().setFeatureInt(Window.FEATURE_PROGRESS,
					newProgress * 100);
			if (newProgress == 100) {
				CookieSyncManager.getInstance().sync();
			}
		}

		@Override
		public void onReceivedTitle(WebView view, String title) {
			setTitle(title);
		}
	}

	final class MyWebViewClient extends WebViewClient {
		@Override
		public boolean shouldOverrideUrlLoading(WebView view, String url) {
			return false;
		}
	}

	public static final String HOME_URI = "file:///android_asset/mob/home.html";

	private WebView webview;

	private MenuItem refreshMenuItem;
	private MenuItem stopMenuItem;
	private MenuItem homeMenuItem;
	private MenuItem backMenuItem;
	private MenuItem forwardMenuItem;

	@Override
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);

		// Call createInstance() explicitly
		CookieSyncManager.createInstance(this);

		requestWindowFeature(Window.FEATURE_PROGRESS);
		requestWindowFeature(Window.FEATURE_LEFT_ICON);

		setContentView(R.layout.webbrowser);
		webview = (WebView) findViewById(R.id.webview);

		getWindow().setFeatureDrawableResource(Window.FEATURE_LEFT_ICON,
				R.drawable.browser);

		webview.setWebChromeClient(new MyWebChromeClient());
		webview.setWebViewClient(new MyWebViewClient());
		//
		WebSettings s = webview.getSettings();
		s.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
		s.setUseWideViewPort(true);
		s.setSavePassword(false);
		s.setSaveFormData(false);
		s.setSupportZoom(true);
		s.setBuiltInZoomControls(true);
		//
		s.setJavaScriptEnabled(true);
		//
		// registerForContextMenu(webview);
		Intent my = getIntent();
		Uri uri = null;
		if (my != null) {
			uri = my.getData();
		}

		if (uri == null) {
			webview.loadUrl(HOME_URI);
		} else {
			webview.loadUrl(uri.toString());
		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			if (webview != null) {
				webview.stopLoading();
			}
			finish();
			return true;
		} else {
			return super.onKeyDown(keyCode, event);
		}
	}

	private void stopLoading() {
		webview.stopLoading();
		Toast toast = Toast.makeText(this, "Stopping", Toast.LENGTH_SHORT);
		toast.show();
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		int id = item.getItemId();
		switch (id) {
		case MENU_HOME:
			webview.loadUrl(HOME_URI);
			break;
		case MENU_REFRESH:
			reload();
			break;
		case MENU_STOP:
			stopLoading();
			break;
		case MENU_BACK:
			webview.goBack();
			break;
		case MENU_FORWARD:
			webview.goForward();
			break;
		default:
			return false;
		}
		return true;
	}

	@Override
	protected void onPause() {
		super.onPause();
	}

	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		homeMenuItem.setEnabled(true);
		refreshMenuItem.setEnabled(true);
		stopMenuItem.setEnabled(true);
		backMenuItem.setEnabled(webview.canGoBack());
		forwardMenuItem.setEnabled(webview.canGoForward());
		return true;
	}

	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState) {
		super.onRestoreInstanceState(savedInstanceState);
		webview.restoreState(savedInstanceState);
	}

	@Override
	protected void onResume() {
		super.onResume();
		CookieSyncManager.getInstance().startSync();
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		webview.saveState(outState);
	}

	@Override
	protected void onStop() {
		super.onStop();

		CookieSyncManager.getInstance().stopSync();
		webview.stopLoading();
	}

	private void reload() {
		webview.reload();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.browser_options, menu);
		//
		refreshMenuItem = menu.findItem(R.id.browser_refresh_opt_menu_id);
		stopMenuItem = menu.findItem(R.id.browser_stop_opt_menu_id);
		homeMenuItem = menu.findItem(R.id.browser_home_opt_menu_id);
		backMenuItem = menu.findItem(R.id.browser_back_opt_menu_id);
		forwardMenuItem = menu.findItem(R.id.browser_forward_opt_menu_id);
		return true;
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		webview.destroy();
	}
}
